package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingPlanDto;
import com.zzyl.dto.NursingProjectDto;
import com.zzyl.entity.NursingPlan;
import com.zzyl.entity.NursingProjectPlan;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.NursingPlanMapper;
import com.zzyl.mapper.NursingProjectPlanMapper;
import com.zzyl.service.NursingPlanService;
import com.zzyl.service.NursingProjectPlanService;
import com.zzyl.vo.NursingPlanVo;
import com.zzyl.vo.NursingProjectPlanVo;
import com.zzyl.vo.NursingProjectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class NursingPlanServiceImpl implements NursingPlanService {

    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;

    @Autowired
    private NursingProjectPlanService nursingProjectPlanService;

    // 分页查询所有
    @Override
    public PageResponse<NursingPlanVo> selectByPage(Integer pageNum, Integer pageSize, String name, Integer status) {
        PageHelper.startPage(pageNum, pageSize);
        Page<List<NursingPlan>> page = nursingPlanMapper.selectByPage(pageNum, pageSize, name, status);

        return PageResponse.of(page, NursingPlanVo.class);
    }

    // 新增护理计划
    @Override
    @Transactional
    public void savePlan(NursingPlanDto nursingPlanDto) {
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtil.copyProperties(nursingPlanDto, nursingPlan);
        nursingPlan.setStatus(1);
        nursingPlan.setCreateBy(1671403256519078138L);
        int count = nursingPlanMapper.countByName(nursingPlan);
        if (count > 0) {
            throw new BaseException(BasicEnum.NURSING_PLAN_INSERT_FAIL_DUPLICATION_NAME);
        }
        // 调用NursingProjectPlanMapper接口，插入计划中的护理项目内容
        if (nursingPlan.getNppList() != null && !nursingPlan.getNppList().isEmpty()) {
            for (NursingProjectPlan npp : nursingPlan.getNppList()) {
                npp.setPlanId(nursingPlan.getId());
            }
            nursingProjectPlanMapper.insertNppList(nursingPlan.getNppList());
        }
        nursingPlanMapper.insertNursingPlan(nursingPlan);
    }

    // 根据id查询护理计划
    @Override
    public NursingPlanVo getById(Long id) {
        int count = nursingPlanMapper.countById(id);
        if (count == 0) {
            throw new BaseException(BasicEnum.NURSING_PLAN_SELECT_ID_FAIL);
        }
        return nursingPlanMapper.getById(id);
    }

    // 删除护理计划
    @Override
    public int removeById(Long id) {
        int count = nursingPlanMapper.countById(id);
        if (count == 0) {
            throw new BaseException(BasicEnum.NURSING_PLAN_SELECT_ID_FAIL);
        }
        // 判断计划是否是启用状态，如果是则不能删除
        int status = nursingPlanMapper.getStatus(id);
        if (status == 1) {
            throw new BaseException(BasicEnum.NURSING_PLAN_DELETE_FAIL);
        }
        return nursingPlanMapper.deleteById(id);
    }

    // 修改护理计划
    @Override
    @Transactional
    public void updateById(NursingPlanDto nursingPlanDto) {
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtil.copyProperties(nursingPlanDto, nursingPlan);
        int count = nursingPlanMapper.countById(nursingPlanDto.getId());
        if (count == 0) {
            throw new BaseException(BasicEnum.NURSING_PLAN_SELECT_ID_FAIL);
        }

        NursingPlanVo byId = getById(nursingPlanDto.getId());
        List<Long> ids = byId.getProjectPlans().stream().map(NursingProjectPlanVo::getId).collect(Collectors.toList());
        nursingProjectPlanService.deleteByIds(ids);
        if (CollUtil.isNotEmpty(nursingPlanDto.getProjectPlans())) {
            nursingPlanDto.getProjectPlans().forEach(v -> v.setPlanId(nursingPlanDto.getId()));
            nursingProjectPlanService.addList(nursingPlanDto.getProjectPlans());
        }

        nursingPlanMapper.updateNursingPlan(nursingPlan);
    }


    // 启用停用
    @Override
    public void isEnable(Long id, Integer status) {
        int count = nursingPlanMapper.countById(id);
        if (count == 0) {
            throw new BaseException(BasicEnum.NURSING_PLAN_SELECT_ID_FAIL);
        }
        nursingPlanMapper.alterStatus(id, status);
    }

    // 查询所有计划
    @Override
    public List<NursingPlanVo> selectAll() {

        return nursingPlanMapper.listAll();
    }




}
